/*
 * postcript.cpp
 *
 *  Created on: Sep 20, 2011
 *      Author: lindenb
 */

#include "postscript.h"
using namespace std;

Postscript::Postscript(ostream& out):_out(out)
	{

	}

Postscript::~Postscript()
	{

	}

void Postscript::escape(char c)
	{
	switch(c)
		{
		case '(': out()<< "\\(";break;
		case ')': out()<< "\\)";break;
		case '\\': out()<< "\\\\";break;
		default:out() << c; break;
		}
	}

Postscript& Postscript::setfont(const char* s,pixel_t size)
	{
	findfont(s);
	scalefont(size);
	return setfont();
	}

Postscript& Postscript::show(const char* s)
	{
	size_t i=0;
	out() << "(";
	while(s[i]!=0)
		{
		escape(s[i]);
		++i;
		}
	out() << ") show" << endl;
	return *this;
	}


Postscript& Postscript::path(const Postscript::Point* pt,std::size_t n)
	{
	newpath();
	for(size_t i=0;i< n;++i)
		{
		if(i==0)
			{
			moveto(pt[i].x,pt[i].y);
			}
		else
			{
			lineto(pt[i].x,pt[i].y);
			}
		}
	closepath();
	return *this;
	}

Postscript& Postscript::path(const std::vector<Point>& points)
	{
	newpath();
	for(size_t i=0;i< points.size();++i)
		{
		if(i==0)
			{
			moveto(points[i].x,points[i].y);
			}
		else
			{
			lineto(points[i].x,points[i].y);
			}
		}
	closepath();
	return *this;
	}


Postscript& Postscript::findfont(const char* font)
	{
	out() << "/"<< font << " findfont\n";
	return *this;
	}

Postscript& Postscript::rect(size_type x,size_type y,size_type width,size_type height)
	{
	moveto(x,y);
	return rect(width,height);
	}
Postscript& Postscript::rect(size_type width,size_type height)
	{
	rlineto(width,0);
	rlineto(0,height);
	rlineto(-width,0);
	return rlineto(0,-height);
	}

Postscript& Postscript::comment(const char* s)
	{
	out() << "%" << s << endl;
	return *this;
	}

Postscript& Postscript::line(size_type x1,size_type y1,size_type x2,size_type y2)
	{
	moveto(x1,y1);
	return lineto(x2,y2);
	}
